package com.slipper.system.codeless.service;

import java.util.Collections;
import java.util.List;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import com.slipper.util.Tool;
import com.slipper.util.UUID;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;

import com.slipper.system.codeless.dao.DataFormAutofillDao;
import com.slipper.system.codeless.bo.DataFormAutofillBO;

import com.slipper.core.transaction.ReadTransactional;
import com.slipper.core.transaction.WriteTransactional;

@Service
@ReadTransactional
public class DataFormAutofillService {
    @Resource
    private DataFormAutofillDao dataFormAutofillDao;
    @Resource
    private DataFormAutofillFieldService dataFormAutofillFieldService;
    
    public DataFormAutofillBO queryBy(String id) {
        return dataFormAutofillDao.queryBy(id);
    }
    
    public List<DataFormAutofillBO> queryList(DataFormAutofillBO bo) {
        return dataFormAutofillDao.queryList(bo);
    }
    
    public PageInfo<DataFormAutofillBO> queryPage(DataFormAutofillBO bo) {
        PageHelper.startPage(bo.getPageNumber(), bo.getPageSize());
        return new PageInfo<>(dataFormAutofillDao.queryList(bo));
    }

    @WriteTransactional
    public int save(String formId, List<DataFormAutofillBO> list, String employeeId) {
        if (Tool.isBlank(formId)) {
            return 0;
        }

        list.forEach(autofill -> {
            autofill.setFormId(formId);
            if (Tool.isBlank(autofill.getId())) {
                autofill.setId(UUID.getUUID());
                autofill.setUpdateId(employeeId);
                autofill.setCreateId(employeeId);
                autofill.setUpdateTime(System.currentTimeMillis());
                autofill.setCreateTime(System.currentTimeMillis());
                dataFormAutofillDao.insert(autofill);
            } else {
                autofill.setUpdateId(employeeId);
                autofill.setUpdateTime(System.currentTimeMillis());
                dataFormAutofillDao.update(autofill);
            }

            dataFormAutofillFieldService.save(autofill.getId(), autofill.getFieldList(), employeeId);
        });

        // 删除被删除的数据
        DataFormAutofillBO qo = new DataFormAutofillBO();
        qo.setFormId(formId);
        dataFormAutofillDao.queryList(qo).stream()
                .filter(autofill -> list.stream().noneMatch(temp -> autofill.getId().equals(temp.getId())))
                .forEach(this::delete);

        return 1;
    }

    @WriteTransactional
    public void saveList(List<DataFormAutofillBO> list) {
        if (list == null) return;

        list.forEach(item -> {
            if (dataFormAutofillDao.queryBy(item.getId()) != null) {
                item.setUpdateTime(System.currentTimeMillis());
                dataFormAutofillDao.update(item);
            } else {
                item.setUpdateTime(System.currentTimeMillis());
                item.setCreateTime(System.currentTimeMillis());
                dataFormAutofillDao.insert(item);
            }
        });
    }

    @WriteTransactional
    public void deleteByFormId(String formId) {
        if (Tool.isBlank(formId)) {
            return;
        }

        DataFormAutofillBO qo = new DataFormAutofillBO();
        qo.setFormId(formId);
        dataFormAutofillDao.queryList(qo).forEach(this::delete);
    }

    @WriteTransactional
    public void delete(DataFormAutofillBO bo) {
        if (Tool.isBlank(bo.getId())) {
            return;
        }

        dataFormAutofillFieldService.deleteByAutofillId(bo.getId());
        dataFormAutofillDao.delete(bo.getId());
    }

    @WriteTransactional
    public void copyByFormId(String oldFormId, String newFormId, String employeeId) {
        if (Tool.isBlank(oldFormId) || Tool.isBlank(newFormId)) {
            return;
        }

        DataFormAutofillBO qo = new DataFormAutofillBO();
        qo.setFormId(oldFormId);
        dataFormAutofillDao.queryList(qo).forEach(autofill -> {
            String oldId = autofill.getId();
            autofill.setId(UUID.getUUID());
            autofill.setFormId(newFormId);
            autofill.setUpdateId(employeeId);
            autofill.setCreateId(employeeId);
            autofill.setUpdateTime(System.currentTimeMillis());
            autofill.setCreateTime(System.currentTimeMillis());
            dataFormAutofillDao.insert(autofill);

            dataFormAutofillFieldService.copyByAutofillId(oldId, autofill.getId(), employeeId);
        });
    }

    public List<DataFormAutofillBO> queryByFormId(String formId) {
        if (Tool.isBlank(formId)) {
            return Collections.emptyList()  ;
        }

        DataFormAutofillBO qo = new DataFormAutofillBO();
        qo.setFormId(formId);
        List<DataFormAutofillBO> list = dataFormAutofillDao.queryList(qo);
        list.forEach(model -> model.setFieldList(dataFormAutofillFieldService.queryByAutofillId(model.getId())));
        return list;
    }
}
